package com.share.domain.activity.service.quota.rule.impl;

import com.share.domain.activity.model.entity.ActivityCountEntity;
import com.share.domain.activity.model.entity.ActivityEntity;
import com.share.domain.activity.model.entity.ActivitySkuEntity;
import com.share.domain.activity.model.valobj.ActivityStateVo;
import com.share.domain.activity.service.quota.rule.AbstractActionChain;
import com.share.types.enums.ResponseCode;
import com.share.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 商品库存规则节点
 */
@Slf4j
@Component("activity_base_action")
public class AbstractBaseActionChain extends AbstractActionChain {
    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        log.info("活动责任链-基础信息【有效期，状态、库存】-校验开始<sku:{},activityId:{}", activitySkuEntity.getSku(),activitySkuEntity.getActivityId());
        if(!ActivityStateVo.open.getCode().equals(activityEntity.getState().getCode())){
            //活动未开启
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(),ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        Date currentDate = new Date();
        if(activityEntity.getBeginDateTime().after(currentDate)||activityEntity.getEndDateTime().before(currentDate)){
            //未在活动时间内
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(),ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }
        if(0>=activitySkuEntity.getStockCountSurplus()){
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(),ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }
        return next().action(activitySkuEntity,activityEntity,activityCountEntity);
    }
}
